
var app = getApp();
var verify = require('verify-token.js');
function request(url, params, contenttype, method, success, fail) {
  this.requestLoading(url, params, contenttype, method, success, fail)
}
// 展示进度条的网络请求
// url:网络请求的url
// params:请求参数
// contenttype:是不是formdata形式
// method:调用方法
// message:进度条的提示信息
// success:成功的回调函数
// fail：失败的回调
function requestLoading(url, params, contenttype, method, message, success, fail) {
  //console.log(params)	
  wx.showNavigationBarLoading();
  if (message != "") {
    wx.showLoading({
      title: message,
    })
  }
	let requestUrl = '';
	if(url.indexOf('app/')>=0){
		requestUrl = app.globalData.appbaseUrl + url.split('app/')[1];
	}else{
		requestUrl = app.globalData.baseUrl + url;
	}
  let token = app.globalData.token;  
  let contentType = '';
  switch (contenttype){
    case 'form':
      contentType =  'application/x-www-form-urlencoded;charset=UTF-8';
			break;
    case 'json':
      contentType = 'application/json';
			break;
  }
  let header = {
    'content-type': contentType,
    'AUTHORIZATION': token
  };
  wx.request({
    url:  requestUrl,
    data: params,
    header: header,
    method: method,
    success: function (res) {
      wx.hideNavigationBarLoading()
      if (message != "") {
        wx.hideLoading()
      }
      if (res.statusCode == 200) {
        success(res.data)
      } else if(res.statusCode==401){
        verify.clearAll();
      }else{
				fail()
			}

    },
    fail: function (res) {
      wx.hideNavigationBarLoading()
      if (message != "") {
        wx.hideLoading()
      }
      fail()
    },
    complete: function (res) {

    },
  })
}
module.exports = {
  request: request,
  requestLoading: requestLoading
}